﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using mshtml;
using Microsoft.Win32;
using WebBrowser = SHDocVw.WebBrowser;


namespace CodeLibrary.BHO
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("D0F7B9B6-51CE-4E22-B242-7BADDF0B3E2F")]
    public class ObjectWithSite : IObjectWithSite, IOleWindow
    {
        private WebBrowser webBrowser;
        //private HTMLDocument document;

        public void OnDocumentCompleted(object pDisp, ref object URL)
        {
            if(URL.ToString().Contains("localhost"))
            {
                //webBrowser.
            }
        }

        public int SetSite(object site)
        {
            if(site != null)
            {
                webBrowser = (WebBrowser) site;
                webBrowser.DocumentComplete += OnDocumentCompleted;
            }
            else
            {
                webBrowser.DocumentComplete -= OnDocumentCompleted;
                webBrowser = null;
            }
            return 0;
        }

        public int GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }

        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            const string KEYNAME = @"Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects";
            RegistryKey k = Registry.LocalMachine.OpenSubKey(KEYNAME, true);
            if(k == null)
            {
                k = Registry.LocalMachine.CreateSubKey(KEYNAME);
            }

            string guid = type.GUID.ToString("B");
            RegistryKey myKey = k.OpenSubKey(guid);
            if(myKey == null)
            {
                myKey = k.CreateSubKey(guid);
                myKey.SetValue("Alright", 1);
            }

            myKey.Close();
            k.Close();
        }

        //[ComUnregisterFunction]
        //public static void UnregisterClass(string key)
        //{
        //    StringBuilder sb = new StringBuilder(key);
        //    sb.Replace(@"HKEY_CLASSES_ROOT\", "");

        //    RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
        //    Debug.Assert(k != null, "k != null");
        //    k.DeleteSubKey("Control", false);
        //    k.OpenSubKey("InprocServer32", true);
        //    k.DeleteSubKey("CodeBase", false);
        //    k.Close();
        //}

        public void GetWindow(out IntPtr phwnd)
        {
            MessageBox.Show("xxxx");
            OleWindow window = new OleWindow();
            phwnd = window.Handle;
        }

        public void ContextSensitiveHelp(bool fEnterMode)
        {
            throw new NotImplementedException();
        }
    }
}
